using UnityEngine;
using System.Collections;

public class DataManager : MonoBehaviour 
{

    public string time;
	private int colorsCollected;		
	private int countCombinations; 
	private int dropCount;

    public Transform details;
    public Transform detailsInstance;
    private int roomNumber;
    private int roomsExplored;
    private ArrayList roomsChecked = new ArrayList();

    private int maxColor;

    private bool ready;
    float counter;
    int second;
    int minutes;

    void Awake()
    {
        
    }

    IEnumerator Start()
    {
        int addctive = 0;

       GameObject gb = Instantiate(Resources.Load("FinalDetails")) as GameObject;
        detailsInstance = gb.transform;

        detailsInstance.parent = Camera.mainCamera.transform;
        detailsInstance.position = new Vector3(Camera.mainCamera.transform.position.x, Camera.mainCamera.transform.position.y,-4);

        detailsInstance.gameObject.SetActive(false);

        if (GameObject.FindGameObjectWithTag("ColorSpecial"))
        {
            addctive = GameObject.FindGameObjectsWithTag("ColorSpecial").Length;

            foreach (GameObject go in GameObject.FindGameObjectsWithTag("ColorSpecial"))
                if (!go.GetComponent<DemoRotate>())
                    go.AddComponent<DemoRotate>();
        }

        this.maxColor = GameObject.FindGameObjectsWithTag("Color").Length + addctive;

        foreach (GameObject go in GameObject.FindGameObjectsWithTag("Color"))
            if (!go.GetComponent<DemoRotate>())
                go.AddComponent<DemoRotate>();
        

        Transform rooms = GameObject.FindGameObjectWithTag("Room").transform;

        foreach (Transform currentChild in rooms.transform)
        {
            roomNumber++;
        }

        roomsExplored++;
        yield return new WaitForSeconds(0.7f);

        ready = true;
    }

    void Update()
    {
        if (ready)
        {
            counter += Time.deltaTime;

            if (counter >= 1)
            {
                if (second < 59)
                {
                    second++;
                }

                else
                {
                    second = 0;
                    minutes++;
                }

                string timer = string.Format("{0:0},{1:00}", minutes, second);
                string[] split = timer.Split(',');

                this.time = minutes.ToString() + ":" + split[1];

                counter = 0;
            }
        }
    }

    void addDrop()
	{
		dropCount ++;
	}
	
	void addCombination()
	{
	    countCombinations++;
	}
	
	void addColor()
	{
		colorsCollected++;
	}

    public void MakeFinal()
    {
        detailsInstance.gameObject.SetActive(true);

        detailsInstance.gameObject.AddComponent("FadeFont");
        detailsInstance.GetComponent<FadeFont>().FadeOut();

        if (detailsInstance != null)
        {
            foreach (Transform t in detailsInstance.transform)
        {

            t.gameObject.AddComponent("FadeFont");
            t.GetComponent<FadeFont>().FadeOut();

            switch (t.name)
            { 
                case "_CubeCollectedValue":
                    t.GetComponent<TextMesh>().text = colorsCollected + "/" + maxColor;
                    break;
                case "_SceneName":
                    t.GetComponent<UI_TextMessage>().id = Application.loadedLevelName;
                    t.GetComponent<UI_TextMessage>().reloadText();
                    break;
                case "_RoomsExploredValue":
                    t.GetComponent<TextMesh>().text = roomsExplored + "/" + roomNumber;
                    break;
                case "_ColorsCombinationValue":
                    t.GetComponent<TextMesh>().text = countCombinations.ToString();
                    break;
                case "_FinalTimeValue":
                    t.GetComponent<TextMesh>().text = time;
                    break;
                case "_CubeDroppedValue":
                    t.GetComponent<TextMesh>().text = dropCount.ToString();
                    break;
            }
        }}


    }

    public void checkFinalRoom()
    {
        System.Text.RegularExpressions.Regex numAlpha = new System.Text.RegularExpressions.Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");

        var match = numAlpha.Match(Application.loadedLevelName);

        int lenght = int.Parse(match.Groups["Numeric"].Value);

        bool needZero = true;

        for (int i = 1; i <= lenght; i++)
        {
            string finalString;

            if (i > 9)
            {
                needZero = false;
            }

            if (needZero)
            {
                finalString = "Level0" + i + "Rooms Explored";
            }
            else
            {
                finalString = "Level" + i + "Rooms Explored";
            }

            if (PlayerPrefs.GetInt(finalString) == 0)
            {
                break;
            }

            if (i == lenght)
            {
                AchievementItem ach = AchieveStorage.getInstance.getByName("Explorer");
                ach.Add();

                if (ach.Teste())
                {
                    AchieveStorage.getInstance.UnlockAchieve(ach, Camera.mainCamera);
                }
            }
        }
    }

    public void receivedRoom(string RoomName)
    {
        foreach (string currentRoom in roomsChecked)
        {
            if (RoomName == currentRoom)
            {
                return;
            }
        }

        roomsExplored++;
        roomsChecked.Add(RoomName);

        if (roomsExplored == roomNumber)
        {
            if (details != null)
            {
                foreach (Transform t in details.transform)
                {
                    switch (t.name)
                    {
                        case "_RoomsExploredValue":
                            t.GetComponent<TextMesh>().text = roomsExplored + "/" + roomNumber;
                            break;
                    }
                }

            }


            PlayerPrefs.SetInt(Application.loadedLevelName + "Rooms Explored", 1);
        }
    }
}
